package br.ufrj.dcc.tp.unitTest.driver;

import java.util.HashMap;

public abstract class GUIDriver {
	private static HashMap<Integer, ThreadApplication> instances;
	private static Integer currentId;
	
	static {
		instances = new HashMap<Integer, ThreadApplication>();
		currentId = 0;
	}
	
	public static MainTest getMainTest(Class <? extends MainTest> klass) {
		try {
			ThreadApplication thread = new ThreadApplication(klass);
			thread.getMainTest().setId(currentId++);
			instances.put(thread.getMainTest().getId(), thread);
			thread.run();
			
			return thread.getMainTest();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void stop(MainTest main) {
		main.stop();
		instances.remove(main.getId());
	}
	
	public static void exit() {
		
		if( instances != null ) {
		
			for( Integer id : instances.keySet() ) {
				instances.get(id).getMainTest().stop();
			}
			instances.clear();
			instances = null;
		}
	}
}
